package com.zhou;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import javax.swing.JOptionPane;

import org.junit.Test;

public class MyAccessDataBase {
	public static String dbpath = "data.accdb";// 路径
	private static String dbname = "";// 用户名
	private static String dbpassword = "";// 密码
	private static String url = "jdbc:access:///" + dbpath;// url
	private static java.sql.Connection conn = null;// 连接

	public MyAccessDataBase() {

	}

	/**
	 * 开始转存数据
	 *
	 * @param fileName
	 * @param sourceTable
	 * @param destTable
	 */
	public static void startTransferData(String fileName, String sourceTable,
										 String destTable) {

		System.out.println(fileName);
		System.out.println(sourceTable);
		System.out.println(destTable);

		Connection connection = null;
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
			connection = DriverManager.getConnection("jdbc:access:///"
					+ fileName, dbname, dbpassword);
		} catch (Exception e) {
			connection = null;
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "数据库连接失败");
			return;
		}

		String sql = "select * from [" + sourceTable + "]";
		try {
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			List<InfoEntity> list = new ArrayList<InfoEntity>();

			while (true) {
				try {
					boolean b = rs.next();
					if (b == false) {
						break;
					}
				} catch (Exception exception) {
					System.out.println("跳过一条错误数据！");
				}
				InfoEntity infoEntity = new InfoEntity();

				infoEntity.inventionName = rs.getString(2);
				infoEntity.applicationNumber = rs.getString(3);
				infoEntity.applicationDate = rs.getString(4);
				infoEntity.applicant = rs.getString(5);
				infoEntity.inventor = rs.getString(6);
				infoEntity.publicAnnouncementNumber = rs.getString(7);
				infoEntity.publicAnnouncementDay = rs.getString(8);
				infoEntity.mainCategoryIPC = rs.getString(9);
				infoEntity.subCategoryIPC = rs.getString(10);
				infoEntity.drugCategoryDOI = rs.getString(11);
				infoEntity.applicantAddress = rs.getString(12);
				infoEntity.summary = rs.getString(13);
				list.add(infoEntity);

				if (list.size() >= 100) {
					forStartTransferData(connection, destTable, list);
					list.clear();
				}
			}
			forStartTransferData(connection, destTable, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public static void forStartTransferData(Connection connection,
											String destTable, List<InfoEntity> list) throws SQLException {
		String sql = "insert into [" + destTable + "] (["
				+ InfoEntity.fieldName[0];
		for (int i = 1; i < 12; i++) {
			sql += "], [" + InfoEntity.fieldName[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < 12; i++) {
			sql += ", ?";
		}
		sql += ")";

		PreparedStatement pstmt = connection.prepareStatement(sql);

		for (int i = 0; i < list.size(); i++) {
			InfoEntity infoEntity = list.get(i);

			pstmt.setString(1, infoEntity.getInventionName());
			pstmt.setString(2, infoEntity.getApplicationNumber());
			pstmt.setString(3, infoEntity.getApplicationDate());
			pstmt.setString(4, infoEntity.getApplicant());
			pstmt.setString(5, infoEntity.getInventor());
			pstmt.setString(6, infoEntity.getPublicAnnouncementNumber());
			pstmt.setString(7, infoEntity.getPublicAnnouncementDay());
			pstmt.setString(8, infoEntity.getMainCategoryIPC());
			pstmt.setString(9, infoEntity.getSubCategoryIPC());
			pstmt.setString(10, infoEntity.getDrugCategoryDOI());
			pstmt.setString(11, infoEntity.getApplicantAddress());
			pstmt.setString(12, infoEntity.getSummary());
			pstmt.addBatch();
		}
		pstmt.executeBatch();
	}

	/**
	 * 添加ip
	 *
	 * @param ip
	 */
	public static void addIP(String ip) {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sqlString = "insert into [代理IP] ([套接字]) values (?)";

		try {
			PreparedStatement pstmt = conn.prepareStatement(sqlString);
			pstmt.setString(1, ip);
			pstmt.executeUpdate();
		} catch (Exception e) {
			if (!e.getMessage().contains("duplicate key")) {
				e.printStackTrace();
			}

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}
		closeConnection();
	}

	public static void updateDownloadPatent(String tableName,
											String applicantNum, String statusValue) {
		String sqlString = "update " + tableName + " set 状态='" + statusValue
				+ "' where 专利号='" + applicantNum + "'";
		try {
			Statement stmtStatement = conn.createStatement();
			stmtStatement.executeUpdate(sqlString);
		} catch (SQLException e) {
			e.printStackTrace();
			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

	}

	public static boolean addData(String[] data) {
		if (data == null) {
			return false;
		}

		String sql = "insert into [FDA专利数据] values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);

			for (int i = 1; i <= data.length; i++) {
				pstmt.setString(i, data[i - 1]);
			}

			pstmt.execute();
			return true;
		} catch (SQLException e) {
			if (e.getMessage().contains("duplicate key")) {
				return true;
			} else {
				System.out.println(e.getMessage());
			}

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		return false;
	}

	/**
	 * 添加下载的数据
	 *
	 * @param data
	 */
	public static boolean addData(Data data) {

		if (data.applicantNum == null || data.applicantNum.equals("")) {
			return false;
		}

		String sqlString = "insert into [FDA专利数据] ([发明名称],[代理人],[申请号],[申请日],[公开(公告)号],"
				+ "[公开(公告)日],[IPC分类号],[ECLA分类号],[申请(专利权)人],[发明人],[优先权号],[优先权日], [CPC分类号],"
				+ "[C-SETS],[摘要]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			PreparedStatement pstmt = conn.prepareStatement(sqlString);

			pstmt.setString(1, data.name);
			pstmt.setString(2, data.proxy);
			pstmt.setString(3, data.applicantNum);
			pstmt.setString(4, data.applicantDay);
			pstmt.setString(5, data.publicNum);
			pstmt.setString(6, data.publicDay);
			pstmt.setString(7, data.ipc);
			pstmt.setString(8, data.ecla);
			pstmt.setString(9, data.applicant);
			pstmt.setString(10, data.inventor);
			pstmt.setString(11, data.priorityNum);
			pstmt.setString(12, data.priorityDay);
			pstmt.setString(13, data.cpc);
			pstmt.setString(14, data.csets);
			pstmt.setString(15, data.summary);

			boolean b = pstmt.execute();
			return b;
		} catch (SQLException e) {

			if (e.getMessage().contains("duplicate key")) {
				return true;
			} else {
				System.out.println(e.getMessage());
			}

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		return false;
	}

	@Test
	public void test() {
		Data data = new Data();
		System.out.println(data);
		addData(data);
	}

	/**
	 * 判断专利号是否已经下载
	 *
	 * @param patentNum
	 * @return
	 */
	public static String getStatusByPatentStatus(String patentNum) {
		String sqlString = "select * from [已下载的专利号] where [专利号]='" + patentNum
				+ "'";
		try {
			Statement stmtStatement = conn.createStatement();
			ResultSet rsResultSet = stmtStatement.executeQuery(sqlString);
			if (rsResultSet.next()) {
				String statuString = rsResultSet.getString(2);

				return statuString;
			}
		} catch (SQLException e) {
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		return "";
	}

	/**
	 * 添加已经下载的专利号
	 *
	 * @param patentNum
	 */
	public static void addDownloadedPatentNum(String patentNum) {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sqlString = "insert into [已下载的专利号] ([专利号]) values ('"
				+ patentNum + "')";

		try {
			Statement stmtStatement = conn.createStatement();
			stmtStatement.executeUpdate(sqlString);
		} catch (SQLException e) {
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		closeConnection();
	}

	public static Set<String> getPatentNum(String tableName, String fieldName) {
		Set<String> patentNumSet = new HashSet<String>();

		String sqlString = "select [" + fieldName + "] from [" + tableName
				+ "]";
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlString);
			while (rs.next()) {
				patentNumSet.add(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		return patentNumSet;
	}

	/**
	 * 连接数据库
	 *
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void connectDataBase(String dbpath)
			throws ClassNotFoundException, SQLException {
		url = "jdbc:access:///" + dbpath;
		Class.forName("com.hxtt.sql.access.AccessDriver");
		conn = DriverManager.getConnection(url, dbname, dbpassword);
	}

	public static boolean testConnectDataBase() {
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
			conn = DriverManager.getConnection(url, dbname, dbpassword);
			System.out.println("没有出错");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			closeConnection();
		}

		return true;
	}

	/**
	 * 连接数据库
	 */
	public static void connectDataBase() {
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
			conn = DriverManager.getConnection(url, dbname, dbpassword);
		} catch (Exception e) {
			conn = null;
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "数据库连接失败");
		}
	}

	public static int getDownloadPage() {
		connectDataBase();

		if (conn == null) {
			return -2;
		}

		String sql = "select * from [下载状态] where id=1";

		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				int curPage = rs.getInt("已下载页数");
				return curPage;
			}
		} catch (SQLException e) {
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		closeConnection();
		return -1;
	}

	/**
	 * 更新下载页
	 *
	 * @param curPage
	 */
	public static int updateDownloadPage(int curPage) {
		connectDataBase();

		if (conn == null) {
			return -1;
		}

		String sql = "select * from [下载状态] where id=1";

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {

				int page = rs.getInt("已下载页数");

				if (curPage <= page) {
					String message = "当前下载页数" + curPage + " <= 已下载页数" + page
							+ "\n是否继续？";
					int showConfirmDialog = JOptionPane.showConfirmDialog(null,
							message);

					if (showConfirmDialog == JOptionPane.YES_OPTION) {
						sql = "update [下载状态] set [已下载页数]=? where id=1";
						pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, curPage);
						pstmt.executeUpdate();
					} else if (showConfirmDialog == JOptionPane.NO_OPTION) {
						return -1;
					} else if (showConfirmDialog == JOptionPane.CANCEL_OPTION) {
						return -1;
					}
				} else {
					sql = "update [下载状态] set [已下载页数]=? where id=1";
					pstmt = conn.prepareStatement(sql);
					pstmt.setInt(1, curPage);
					pstmt.executeUpdate();
				}
			} else {
				sql = "insert into [下载状态] values (1, ?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, curPage);
				pstmt.execute();
			}
		} catch (SQLException e) {
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
			}
		}

		closeConnection();
		return 1;
	}

	public static int insertInfoList(String tableName, List<InfoEntity> infoList) {

		if (infoList.isEmpty()) {
			return 1;
		}

		connectDataBase();

		if (conn == null) {
			return -1;
		}

		String sql = "insert into [" + tableName + "] (["
				+ InfoEntity.fieldName[0];
		for (int i = 1; i < 12; i++) {
			sql += "], [" + InfoEntity.fieldName[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < 12; i++) {
			sql += ", ?";
		}
		sql += ")";

		// System.out.println(sql);

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			conn.setAutoCommit(false);

			for (int i = 0; i < infoList.size(); i++) {
				InfoEntity infoEntity = infoList.get(i);

				// if (isRepeat(tableName, infoEntity.getInventionName(),
				// infoEntity.getApplicationNumber())) {
				// continue;
				// }

				pstmt.setString(1, infoEntity.getInventionName());
				pstmt.setString(2, infoEntity.getApplicationNumber());
				pstmt.setString(3, infoEntity.getApplicationDate());
				pstmt.setString(4, infoEntity.getApplicant());
				pstmt.setString(5, infoEntity.getInventor());
				pstmt.setString(6, infoEntity.getPublicAnnouncementNumber());
				pstmt.setString(7, infoEntity.getPublicAnnouncementDay());
				pstmt.setString(8, infoEntity.getMainCategoryIPC());
				pstmt.setString(9, infoEntity.getSubCategoryIPC());
				pstmt.setString(10, infoEntity.getDrugCategoryDOI());
				pstmt.setString(11, infoEntity.getApplicantAddress());
				pstmt.setString(12, infoEntity.getSummary());
				pstmt.addBatch();
			}

			pstmt.executeBatch();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();

			if (e.getMessage().contains("Failed to find table")) {
				JOptionPane.showMessageDialog(null, "表不存在");
				return -2;
			} else {
				// 数据库异常
				JOptionPane.showMessageDialog(null, "数据库异常：" + e.getMessage());
				return -3;
			}
		}

		closeConnection();
		return 1;
	}

	/**
	 * 详览表
	 *
	 * @param tableName
	 * @param detailList
	 */
	public static void insertDetailList(String tableName,
										List<InfoEntity> detailList) {

		if (detailList.isEmpty()) {
			return;
		}

		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "insert into [" + tableName + "] (["
				+ InfoEntity.fieldNameDetail[0];
		for (int i = 1; i < InfoEntity.fieldNameDetail.length; i++) {
			sql += "], [" + InfoEntity.fieldNameDetail[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < InfoEntity.fieldNameDetail.length; i++) {
			sql += ", ?";
		}
		sql += ")";

		// System.out.println(sql);

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			conn.setAutoCommit(false);

			for (int i = 0; i < detailList.size(); i++) {
				InfoEntity infoEntity = detailList.get(i);

				pstmt.setString(1, infoEntity.getInventionName());
				pstmt.setString(2, infoEntity.getApplicationNumber());
				pstmt.setString(3, infoEntity.getApplicationDate());
				pstmt.setString(4, infoEntity.getApplicant());
				pstmt.setString(5, infoEntity.getInventor());
				pstmt.setString(6, infoEntity.getPublicAnnouncementNumber());
				pstmt.setString(7, infoEntity.getPublicAnnouncementDay());
				pstmt.setString(8, infoEntity.getPriorityNumber());
				pstmt.setString(9, infoEntity.getPriorityDate());
				pstmt.setString(10, infoEntity.getApplicantAddress());
				pstmt.setString(11, infoEntity.getPriorityZipCode());
				pstmt.setString(12, infoEntity.getSummary());
				pstmt.addBatch();
			}

			pstmt.executeBatch();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 引证表
	 *
	 * @param tableName
	 * @param quoteList
	 */
	public static void insertQuoteList(String tableName, List<Quote> quoteList) {

		if (quoteList.isEmpty()) {
			return;
		}

		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "insert into [" + tableName + "] ([" + Quote.fieldName[0];
		for (int i = 1; i < Quote.fieldName.length; i++) {
			sql += "], [" + Quote.fieldName[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < Quote.fieldName.length; i++) {
			sql += ", ?";
		}
		sql += ")";

		// System.out.println(sql);

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			conn.setAutoCommit(false);

			for (int i = 0; i < quoteList.size(); i++) {
				Quote quote = quoteList.get(i);

				pstmt.setString(1, quote.getSourceName());
				pstmt.setString(2, quote.getSourceApplicationNumber());
				pstmt.setString(3, quote.getRelevance());
				pstmt.setString(4, quote.getPublicNumberString());
				pstmt.setString(5, quote.getApplicationNumberString());
				pstmt.setString(6, quote.getIpc());
				pstmt.setString(7, quote.getApplicant());
				pstmt.setString(8, quote.getInventor());
				pstmt.setString(9, quote.getName());
				pstmt.setString(10, quote.getRelatedPara());
				pstmt.setString(11, quote.getRequirementString());
				pstmt.addBatch();
			}

			pstmt.executeBatch();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 同族表
	 *
	 * @param tableName
	 * @param kinList
	 */
	public static void insertKinList(String tableName, List<Kin> kinList) {

		if (kinList.isEmpty()) {
			return;
		}

		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "insert into [" + tableName + "] ([" + Kin.fieldName[0];
		for (int i = 1; i < Kin.fieldName.length; i++) {
			sql += "], [" + Kin.fieldName[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < Kin.fieldName.length; i++) {
			sql += ", ?";
		}
		sql += ")";

		// System.out.println(sql);

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			conn.setAutoCommit(false);

			for (int i = 0; i < kinList.size(); i++) {
				Kin kin = kinList.get(i);

				pstmt.setString(1, kin.getSourceName());
				pstmt.setString(2, kin.getSourceApplicationNumber());
				pstmt.setString(3, kin.getGroupNumberString());
				pstmt.setString(4, kin.getPublicNumberString());
				pstmt.setString(5, kin.getPublicDateString());
				pstmt.setString(6, kin.getApplicationNumberString());
				pstmt.setString(7, kin.getPriorityNumber());
				pstmt.setString(8, kin.getNameString());
				pstmt.addBatch();
			}

			pstmt.executeBatch();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 法律状态表
	 *
	 * @param tableName
	 * @param lawStateList
	 */
	public static void insertLawStateList(String tableName,
										  List<LawState> lawStateList) {

		if (lawStateList.isEmpty()) {
			return;
		}

		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "insert into [" + tableName + "] (["
				+ LawState.fieldName[0];
		for (int i = 1; i < LawState.fieldName.length; i++) {
			sql += "], [" + LawState.fieldName[i];
		}
		sql += "]) values (?";
		for (int i = 1; i < LawState.fieldName.length; i++) {
			sql += ", ?";
		}
		sql += ")";

		// System.out.println(sql);

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			conn.setAutoCommit(false);

			for (int i = 0; i < lawStateList.size(); i++) {
				LawState kin = lawStateList.get(i);

				pstmt.setString(1, kin.getApplicationNumber());
				pstmt.setString(2, kin.getEffectiveDate());
				pstmt.setString(3, kin.getMeaningString());
				pstmt.addBatch();
			}

			pstmt.executeBatch();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
		}

		closeConnection();
	}

	public static boolean isRepeat(String tableName, String inventionName,
								   String applicationNumber) {
		String sql = "select count(*) from [" + tableName
				+ "] where [发明名称]=? and [申请号]=?";
		// System.out.println(sql);
		boolean b = false;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, inventionName);
			pstmt.setString(2, applicationNumber);
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			if (rs.getInt(1) > 0) {
				b = true;
			}
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
		}
		return b;
	}

	/**
	 * 创建表
	 *
	 * @param names
	 * @param tableName
	 */
	public static void createTable(String[] names, String tableName) {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "create table if not exists [" + tableName
				+ "] (id int auto_increment primary key";

		for (int i = 0; i < names.length - 1; i++) {
			sql += ", [" + names[i] + "] varchar(200)";
		}

		sql += ", [" + names[names.length - 1] + "] varchar(1000))";

		System.out.println(sql);

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 创建引证表
	 */
	public void createTableQuote() {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "create table if not exists [引证] (id int auto_increment primary key, "
				+ "[源发明名称] varchar(200), [源申请号] varchar(200), [相关性] varchar(200), [公开号] varchar(200), [申请号] varchar(200), [IPC分类号] varchar(200), [申请人] varchar(200), [发明人] varchar(200), [发明名称] varchar(200), [相关段落] varchar(200), [相关权利要求] varchar(200))";

		System.out.println(sql);

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 创建同族表
	 */
	public void createTableKin() {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "create table if not exists [同族] (id int auto_increment primary key, "
				+ "[源发明名称] varchar(200), [源申请号] varchar(200), [族号] varchar(200), [公开(公告)号] varchar(200), [公开(公告)日] varchar(200), [申请号] varchar(200), [优先权号] varchar(200), [发明名称] varchar(200))";

		System.out.println(sql);

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 创建法律状态表
	 */
	public void createTableLawState() {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "create table if not exists [法律状态] (id int auto_increment primary key, "
				+ "[申请号] varchar(200), [法律状态生效日] varchar(200), [法律状态含义] varchar(200))";

		System.out.println(sql);

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 创建详览表
	 */
	public void createTableDetail() {
		connectDataBase();

		if (conn == null) {
			return;
		}

		String sql = "create table if not exists [详览] (id int auto_increment primary key, "
				+ "[发明名称] varchar(200), [申请号] varchar(200), [申请日] varchar(200), "
				+ "[申请(专利权)人] varchar(200), [发明人] varchar(200), [公开(公告)号] varchar(200), "
				+ "[公开(公告)日] varchar(200), [IPC分类号] varchar(200), [优先权号] varchar(200), "
				+ "[优先权日] varchar(200), [申请人地址] varchar(200), [申请人邮编] varchar(200),"
				+ "[CPC分类号] varchar(200), [摘要] varchar(200))";

		System.out.println(sql);

		try {
			Statement stmt = conn.createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		closeConnection();
	}

	/**
	 * 关闭数据库
	 */
	public static void closeConnection() {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Test
	public void test1() {
		createTable(InfoEntity.fieldName, "表二");
		createTable(InfoEntity.fieldName, "表三");
		createTable(InfoEntity.fieldName, "表四");

		createTableLawState();
		createTableQuote();
		createTableKin();
		createTableDetail();

		// List<InfoEntity> infoList = new ArrayList<InfoEntity>();
		// infoList.add(new InfoEntity());
		// insertInfoList("表一", infoList);
	}
}
